﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Reflection;
using Assist;
using MySql.Data.MySqlClient;

namespace YJKRevitFamilyDataBase
{
	public class MySqlFamilyFileTable : IFamilyFileTable
	{
		public MySqlFamilyFileTable(FamilyLibDatabase db)
		{
			this.mDatabase = db;
		}

		public void Update()
		{
		}

		public bool Add(DbFamily family, FileInfo fi, DbTransaction tr)
		{
			byte[] array = Assist.FileUtil.ReadFile(fi.FullName);
			bool result;
			try
			{
				using (MySqlCommand mySqlCommand = ((MySqlConnection)this.mDatabase.Connection).CreateCommand())
				{
					mySqlCommand.Transaction = (MySqlTransaction)tr;
					mySqlCommand.CommandText = string.Format("insert into FamilyFileTable (FamilyGUID,FamilyFile) values ('{0}',{1})", family.Guid.ToString(), "?file");
					MySqlParameter mySqlParameter = new MySqlParameter("?file", MySqlDbType.Blob, array.Length);
					mySqlParameter.Value = array;
					mySqlCommand.Parameters.Add(mySqlParameter);
					mySqlCommand.ExecuteNonQuery();
				}
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public bool Remove(DbFamily family, DbTransaction tr)
		{
			bool result;
			try
			{
				this.mDatabase.ExecuteCommand("delete from FamilyFileTable where FamilyGUID='" + family.Guid.ToString() + "'", tr);
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public FileInfo this[DbFamily family]
		{
			get
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string text = Assembly.GetExecutingAssembly().Location;
				int num = text.LastIndexOf("\\");
				text = text.Substring(0, num + 1);
				FileInfo result = null;
				DataTable dataTable = this.mDatabase.ExecQuery("select FamilyFile from FamilyFileTable where FamilyGUID='" + family.Guid.ToString() + "'");
				DataTable dataTable2 = this.mDatabase.ExecQuery("select FamilyName from FamilyTable where GUID='" + family.Guid.ToString() + "'");
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				if (dataTable != null && dataTable2 != null)
				{
					byte[] pReadByte = (byte[])dataTable.Rows[0][0];
					string fileName = text + "\\HY_" + dataTable2.Rows[0][0].ToString() + ".rfa";
					Assist.FileUtil.WriteFile(pReadByte, fileName);
					result = new FileInfo(fileName);
				}
				return result;
			}
		}

		public bool Replace(DbFamily family, FileInfo fi)
		{
			if (this.mDatabase.Connection.State == ConnectionState.Closed)
			{
				this.mDatabase.Connection.Open();
			}
			DbTransaction dbTransaction = this.mDatabase.StartTransaction();
			byte[] value = Assist.FileUtil.ReadFile(fi.FullName);
			bool result;
			try
			{
				using (MySqlCommand mySqlCommand = ((MySqlConnection)this.mDatabase.Connection).CreateCommand())
				{
					mySqlCommand.Transaction = (MySqlTransaction)dbTransaction;
					mySqlCommand.CommandText = string.Format("update FamilyFileTable set FamilyFile={0} where FamilyGUID='{1}'", "?file", family.Guid.ToString());
					MySqlParameter mySqlParameter = new MySqlParameter("?file", MySqlDbType.Blob);
					mySqlParameter.Value = value;
					mySqlCommand.Parameters.Add(mySqlParameter);
					mySqlCommand.ExecuteNonQuery();
				}
				this.mDatabase.CommitTransaction(dbTransaction);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				result = true;
			}
			catch (Exception ex)
			{
				this.mDatabase.RollbackTransaction(dbTransaction);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public Dictionary<string, Dictionary<string, byte[]>> GetAllFileInfos()
		{
			Dictionary<string, Dictionary<string, byte[]>> dictionary = new Dictionary<string, Dictionary<string, byte[]>>();
			if (this.mDatabase.Connection.State == ConnectionState.Closed)
			{
				this.mDatabase.Connection.Open();
			}
			DataTable dataTable = this.mDatabase.ExecQuery("select FamilyFile,FamilyGUID from FamilyFileTable");
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					string text = (string)dataTable.Rows[i][1];
					Dictionary<string, byte[]> dictionary2 = new Dictionary<string, byte[]>();
					byte[] value = (byte[])dataTable.Rows[i][0];
					DataTable dataTable2 = this.mDatabase.ExecQuery("select FamilyName from FamilyTable where GUID='" + text + "'");
					dictionary2.Add((string)dataTable2.Rows[0][0], value);
					dictionary.Add(text, dictionary2);
				}
			}
			if (this.mDatabase.Connection.State == ConnectionState.Open)
			{
				this.mDatabase.Connection.Close();
			}
			return dictionary;
		}

		public bool Replace(string guid, byte[] fileBytes)
		{
			if (this.mDatabase.Connection.State == ConnectionState.Closed)
			{
				this.mDatabase.Connection.Open();
			}
			DbTransaction dbTransaction = this.mDatabase.StartTransaction();
			bool result;
			try
			{
				using (MySqlCommand mySqlCommand = ((MySqlConnection)this.mDatabase.Connection).CreateCommand())
				{
					mySqlCommand.Transaction = (MySqlTransaction)dbTransaction;
					mySqlCommand.CommandText = string.Format("update FamilyFileTable set FamilyFile={0} where FamilyGUID='{1}'", "?file", guid);
					MySqlParameter mySqlParameter = new MySqlParameter("?file", MySqlDbType.Blob);
					mySqlParameter.Value = fileBytes;
					mySqlCommand.Parameters.Add(mySqlParameter);
					mySqlCommand.ExecuteNonQuery();
				}
				this.mDatabase.CommitTransaction(dbTransaction);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				result = true;
			}
			catch (Exception ex)
			{
				this.mDatabase.RollbackTransaction(dbTransaction);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		private FamilyLibDatabase mDatabase;
	}
}
